﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace produccion.clases
{
  public  class CLSTABDAN
    {
        string secdan_dan="", coddan_dan="", nomdan_dan="";
        double costo_dan;
        MDOgeneral mod = new MDOgeneral();

        public string SECDAN_DAN
        {
            get { return secdan_dan; }
            set { secdan_dan = value; }
        }

        public string CODDAN_DAN
        {
            get { return coddan_dan; }
            set { coddan_dan = value; }
        }

        public string NOMDAN_DAN
        {
            get { return nomdan_dan; }
            set { nomdan_dan = value; }
        }

        public double COSTO_DAN
        {
            get { return costo_dan; }
            set { costo_dan = value; }
        }

        public List<CLSTABDAN> listarDaños()
        {

            try
            {
                string query = "select SECDAN_DAN, CODDAN_DAN, TRIM(NOMDAN_DAN) as NOMDAN_DAN, COSTO_DAN from tabdan order by NOMDAN_DAN";
                DataTable val = mod.consulta(query);
                List<CLSTABDAN> lisdan = new List<CLSTABDAN>();
                foreach (DataRow r in val.Rows)
                {
                    lisdan.Add(new CLSTABDAN
                    {
                        SECDAN_DAN = r["SECDAN_DAN"].ToString(),
                        NOMDAN_DAN = r["NOMDAN_DAN"].ToString(),
                        CODDAN_DAN = r["SECDAN_DAN"].ToString().Trim() + r["CODDAN_DAN"].ToString().Trim() ,
                        COSTO_DAN =  Convert.ToDouble(r["COSTO_DAN"])
                    });
                }
                return lisdan;
            }
            catch (Exception ex) { throw (ex); }
        }

        public DataTable listarDano() 
        {
            try 
            {
                string query = "select TRIM(NOMDAN_DAN) as NOMDAN_DAN,TRIM(CODDAN_DAN) as CODDAN_DAN from TABDAN where SECDAN_DAN='" + SECDAN_DAN + "' order by NOMDAN_DAN";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }
      

        public DataTable listarAllDano()
        {
            try
            {
                string query = "select TRIM(NOMDAN_DAN) as NOMDAN_DAN,TRIM(CODDAN_DAN) as CODDAN_DAN from TABDAN order by NOMDAN_DAN";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }



        public int Insertar()
        {
            try
            {
                string query = "INSERT INTO TABDAN(SECDAN_DAN, CODDAN_DAN, NOMDAN_DAN, COSTO_DAN) VALUES ('" + SECDAN_DAN + "','" + CODDAN_DAN + "','" + NOMDAN_DAN + "'," + COSTO_DAN + ")";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Actualizar()
        {
            try
            {
                string query = "update tabdan set nomdan_dan='" + NOMDAN_DAN + "',costo_dan=" + COSTO_DAN + " where secdan_dan='" + SECDAN_DAN + "' and coddan_dan='" + CODDAN_DAN + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Eliminar()
        {
            try
            {
                string query = "delete from tabdan where secdan_dan='" + SECDAN_DAN + "' and coddan_dan='" + CODDAN_DAN + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable Secciones()
        {
            try
            {
                string query = "select codsec_sec, nomsec_sec from maesec order by nomsec_sec";
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable Consultar()
        {
            try
            {                
                string query = "select coddan_dan, nomdan_dan, costo_dan from tabdan where secdan_dan='" + SECDAN_DAN + "'";
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Boolean Existe()
        {
            try
            {
                string query = "select coddan_dan from tabdan where secdan_dan='" + SECDAN_DAN + "' and coddan_dan='" + CODDAN_DAN + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
